package com.jming.yygh.user.api;

import com.jming.yygh.common.result.Result;
import com.jming.yygh.common.util.AuthContextHolder;
import com.jming.yygh.user.service.UserInfoService;
import com.jming.yygh.vo.user.LoginVo;
import com.jming.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author jming
 * @version 0.0.1
 * @date 2021/05/08
 */
@Api(tags = "用户登录管理接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户登录接口
     * @param loginVo 用户信息
     * @return Result 登录结果
     */
    @ApiOperation(value = "用户登录接口")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> info = userInfoService.loginUser(loginVo);
        return Result.ok(info);
    }

    /**
     * 用户认证接口
     * @param userAuthVo 用户信息
     * @param request 请求信息
     * @return Result
     */
    @ApiOperation(value = "用户认证接口")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {

        // 传递两个参数 第一个参数用户id，第二个参数认证数据的VO对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);

        return Result.ok();
    }

    /**
     * 根据id获取用户信息接口
     * @param request
     * @return Result
     */
    @ApiOperation(value = "根据id获取用户信息接口")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {

        Long userId = AuthContextHolder.getUserId(request);

        return Result.ok(userInfoService.getById(userId));
    }

}
